package com.ww.activiti.service.impl;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ww.orm.entity.*;
import com.ww.activiti.service.HistoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: sunnannan
 */
@Service
public class HistoryInfoServiceImpl implements HistoryInfoService {

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public List<ActHiTaskinstEntity> myTasksCompleted(String userId) {
        List<String> dealUserId = jpaQueryFactory.selectFrom(QActHiVarinstEntity.actHiVarinstEntity)
                .where(QActHiVarinstEntity.actHiVarinstEntity.name.eq("dealUserId")
                        .and(QActHiVarinstEntity.actHiVarinstEntity.text.eq(userId))).fetch().stream()
                .map(ActHiVarinstEntity::getTaskId).collect(Collectors.toList());
        if (dealUserId.size() < 1) {
            return null;
        }
        return jpaQueryFactory.selectFrom(QActHiTaskinstEntity.actHiTaskinstEntity)
                .where(QActHiTaskinstEntity.actHiTaskinstEntity.id.in(dealUserId)).fetch();
    }

    @Override
    public List<ActHiProcinstEntity> myProcessStarted(String userId) {
        List<String> startUserId = jpaQueryFactory.selectFrom(QActHiVarinstEntity.actHiVarinstEntity)
                .where(QActHiVarinstEntity.actHiVarinstEntity.name.eq("startUserId")
                        .and(QActHiVarinstEntity.actHiVarinstEntity.text.eq(userId))).fetch().stream()
                .map(ActHiVarinstEntity::getProcInstId).collect(Collectors.toList());

        if (startUserId.size() < 1) {
            return null;
        }
        return jpaQueryFactory.selectFrom(QActHiProcinstEntity.actHiProcinstEntity)
                .where(QActHiProcinstEntity.actHiProcinstEntity.procInstId.in(startUserId)).fetch();
    }
}
